package com.lznAdmin.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lznAdmin.base.lznAdminResult;
import com.lznAdmin.system.entity.SysRole;
import com.lznAdmin.system.entity.SysRolePermission;
import com.lznAdmin.system.entity.SysUserRole;
import com.lznAdmin.system.mapper.SysUserRoleMapper;
import com.lznAdmin.system.service.SysUserRoleService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @program: lzn-manage-parent
 * @description: 角色管理
 * @author: lzn
 * @create: 2021-09-01 15:03
 **/
@RestController
@RequestMapping("/sys/role")
@Slf4j
public class SysRoleController extends BaseController {

    @Autowired
    SysUserRoleService sysUserRoleService;

    @Autowired
    SysUserRoleMapper sysUserRoleMapper;


    @GetMapping("/roleselectoptions")
    public lznAdminResult getRoleSelectOptions() {
        List<SysRole> roleList = sysRoleService.list();
        return lznAdminResult.ok(roleList);
    }

    @ApiOperation(value = "查询角色列表分页数据")
    @GetMapping("/list")
    public lznAdminResult getRoleList(SysRole sysRole,
                                      @RequestParam(name = "current", defaultValue = "1") Integer current,
                                      @RequestParam(name = "size", defaultValue = "10") Integer size,
                                      HttpServletRequest req) {
        log.info("sysRole = {}", sysRole);
        Page<SysRole> page = new Page<>(current, size);
        Map<String, String[]> parameterMap = req.getParameterMap();
        // log.info(parameterMap.get("begin_date")[0].toString());
        // log.info(parameterMap.get("end_date")[0].toString());
        QueryWrapper<SysRole> sysRoleQueryWrapper = queryWrapperUtil.BuildQueryWrapper(sysRole, req.getParameterMap());
        IPage<SysRole> sysRoleIPage = sysRoleService.page(page, sysRoleQueryWrapper);
        return lznAdminResult.ok(sysRoleIPage);
    }

    @ApiOperation(value = "新增角色")
    @PostMapping("/add")
    public lznAdminResult addRole(@Validated @RequestBody SysRole sysRole) {
        log.info("addRole={}", sysRole.toString());
        boolean isSave = sysRoleService.save(sysRole);
        if (isSave) {
            return lznAdminResult.ok("保存成功！");
        }
        return lznAdminResult.no("保存失败！");
    }


    // @PreAuthorize("hasAuthority('sys:user:update')")
    @ApiOperation(value = "编辑角色")
    @PutMapping("/edit")
    public lznAdminResult editRole(@Validated @RequestBody SysRole sysRole) throws Exception {
        log.info("editRole = {}", sysRole.toString());
        boolean isUpdate = sysRoleService.updateById(sysRole);
        if (isUpdate) {
            return lznAdminResult.ok("保存成功！");
        }
        return lznAdminResult.no("保存失败！");
    }

    @ApiOperation(value = "删除角色")
    @DeleteMapping("/del")
    public lznAdminResult deleteRole(@RequestParam(name = "id") String id) {
        log.info("deleteId = {}", id);
        boolean isDelete = sysRoleService.removeById(id);
        if (isDelete) {
            return lznAdminResult.ok("删除成功");
        }
        return lznAdminResult.no("删除失败");
    }

    @ApiOperation(value = "获取已分配菜单列表")
    @GetMapping("/AssignedMenuList")
    public lznAdminResult getAssignedMenuListByRoleId(@RequestParam(name = "id") Long id) {
        log.info(id.toString());
        List<Long> permissionIdByRoleId = sysRoleService.getPermissionIdByRoleId(id);
        return lznAdminResult.ok(permissionIdByRoleId);
    }

    @ApiOperation(value = "更新已分配菜单列表")
    @PutMapping("/EditAssignedMenu")
    public lznAdminResult editAssignedMenuList(@RequestBody List<SysRolePermission> sysRolePermissionList) {
        log.info(sysRolePermissionList.toString());
        Boolean aBoolean = sysRolePermissionService.updatePerIdsById(sysRolePermissionList);
        if (aBoolean) {
            return lznAdminResult.ok("保存成功！");
        }
        return lznAdminResult.no("保存失败！");
    }


    @ApiOperation(value = "获取已分配用户列表")
    @GetMapping("/AssignedUserList")
    public lznAdminResult getAssignedUserByRoleId(@RequestParam(name = "roleId") Long roleId) {
        QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", roleId);
        List<SysUserRole> list = sysUserRoleService.list(queryWrapper);
        return lznAdminResult.ok(list);
    }

    @ApiOperation(value = "更新已分配用户列表")
    @PutMapping("/editAssignedUserList")
    public lznAdminResult editAssignedUser(@RequestBody List<SysUserRole> sysUserRoleList) {
        log.info(sysUserRoleList.toString());
        Boolean aBoolean = sysUserRoleService.roleReassignUser(sysUserRoleList);
        if (aBoolean) {
            return lznAdminResult.ok("保存成功！");
        }
        return lznAdminResult.no("保存失败！");
    }
}
